package com.cloud.custom.tasklist.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.custom.common.result.QkIcon;
import com.cloud.custom.common.result.QkTreeNode;
import com.cloud.custom.common.result.Quokka;
import com.cloud.custom.common.utils.TreeHelper;
import com.cloud.custom.interfaces.FeignClientFreeflow;
import com.cloud.custom.tasklist.bean.ActDef;
import com.cloud.custom.tasklist.bean.MenuTabAuthority;
import com.cloud.custom.tasklist.bean.Part;
import com.cloud.custom.tasklist.bean.ProcDef;
import com.cloud.custom.tasklist.entity.TaskListMenu;
import com.cloud.custom.tasklist.entity.TaskListMenuGroup;
import com.cloud.custom.tasklist.service.TaskListMenuService;

@RestController
@RequestMapping("/custom/api/tasklist/menu")
public class TaskListMenuController {

	@Autowired
	private TaskListMenuService taskListMenuService;

	@Autowired
	private FeignClientFreeflow feignClientFreeflow;

	@GetMapping("/listtree")
	public Quokka listTree() {
		return new Quokka(true, taskListMenuService.listTree());
	}

	@RequestMapping("/groups")
	public Quokka listGroups() {
		return new Quokka(true, taskListMenuService.listTaskListMenuHeader());
	}

	@RequestMapping("/listauthority")
	public Quokka listPartAuthority(long taskListId, long actDefId) {
		List<Part> parts = feignClientFreeflow.loadPartsByActDef(actDefId);
		return new Quokka(true, taskListMenuService.listPartAuthority(taskListId, actDefId, parts));
	}

	@RequestMapping("/saveupdateauth")
	public Quokka saveUpdatePartAuth(@RequestBody List<MenuTabAuthority> parts, long actDefId, long taskListId) {
		taskListMenuService.saveUpdatePartAuth(parts, actDefId, taskListId);
		return new Quokka(true);
	}
	
	/**
	 * 获取在指定任务栏、指定环节、位置的操作项信息
	 * @param taskListId 任务栏
	 * @param actDefId 环节
	 * @param poz 位置
	 * @return
	 */
	@RequestMapping("/listbypart")
	public Quokka listTaskListMenuPart(long taskListId, long actDefId, Integer poz) {
		return new Quokka(true, taskListMenuService.listTaskListMenuByPart(taskListId, actDefId, poz));
	}

	@PostMapping("/group/saveupdate")
	public Quokka saveUpdateGroup(@RequestBody TaskListMenuGroup group) {
		return new Quokka(true, taskListMenuService.saveUpdateGroup(group));
	}

	@GetMapping("/group/delete")
	public Quokka deleteGroup(long groupId) {
		taskListMenuService.deleteGroup(groupId);
		return new Quokka(true);
	}
	
	@PostMapping("saveupdate")
	public Quokka saveUpdateMenu(@RequestBody TaskListMenu menu){
		return new Quokka(true, taskListMenuService.saveUpdateTaskListMenu(menu));
	}
	
	@GetMapping("delete")
	public Quokka deleteMenu(long menuId){
		return new Quokka(true, taskListMenuService.deleteMenu(menuId));
	}
	
	@GetMapping("load")
	public Quokka loadMenu(long menuId){
		return new Quokka(true, taskListMenuService.loadMenu(menuId));
	}
	
	@GetMapping("listprocactparts")
	public Quokka listProcActParts(int bizId){
		List<ProcDef> procs = this.feignClientFreeflow.loadProcActParts(bizId);
		List<QkTreeNode> tree = new ArrayList<QkTreeNode>();
		for(ProcDef proc : procs){
			QkTreeNode procNode = new QkTreeNode("p_" + proc.getProcDefId(), proc.getProcDefName()); 
			procNode.setRootFlag(true);
			procNode.setSelectable(false);
			procNode.setExpanded(true);
			procNode.setIcon(QkIcon.FA_LAPTOP);
			procNode.setSeniorId("");
			tree.add(procNode);
			for(ActDef act : proc.getActDefs()){
				QkTreeNode actNode = new QkTreeNode("a_" + act.getActDefId(), act.getActDefName()); 
				actNode.setLeaf(true);
				actNode.setSeniorId("p_" + proc.getProcDefId());
				actNode.setSelectable(true);
				actNode.setIcon(QkIcon.FA_FILE_WORD);
				Map<String, Object> data = new HashMap<String, Object>();
				data.put("parts", act.getActParts());
				actNode.setData(data);
				tree.add(actNode);
			}
		}
		return new Quokka(true, TreeHelper.buildList2Tree(tree));
	}
}
